package com.itbaizhan.controller;

import com.itbaizhan.constant.Constant;
import com.itbaizhan.dto.LoginDto;
import com.itbaizhan.dto.Result;
import com.itbaizhan.service.LoginUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * @author spf
 * @date 2024/5/4
 * @time 10:57
 */
@Controller
@RequestMapping("/shopping")
public class LoginControler {

    @Autowired
    private LoginUserService loginUserService;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody LoginDto loginDto) {
//        LoginUser loginUser = loginUserService.getLoginUser(loginDto.getUsername());
//        if (loginUser == null){
//            throw new RuntimeException("用户不存在");
//        }
//
//        if (!loginUser.getPassword().equals(loginDto.getPassword())){
//            throw new RuntimeException("密码输入错误");
//        }
        String client_id = Constant.client_id;
        String client_secret = Constant.client_secret;
        String grant_type_password = Constant.grant_type_password;

        URI uri = URI.create(Constant.getTokenUrl + "?client_id=" + client_id + "&client_secret=" + client_secret + "&grant_type=" + grant_type_password + "&username=" + loginDto.getUsername() + "&password=" + loginDto.getPassword());

        Result result = restTemplate.getForObject(uri, Result.class);

        return result;
    }
}
